package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.HouseKeepingUserLogic;

import data.HouseKeepingUser;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class HouseKeepingUserService extends HttpServlet{
	//http://localhost:8080/housekeepinguserService?func=GetStaff&department=HouseKeeping
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("add")){
				AddHouseKeeping(req,resp);
			}else if(doClass.equals("edit")){
				EditHouseKeeping(req,resp);
			}else if(doClass.equals("del")){
				DeleteHouseKeeping(req,resp);
			}else if(doClass.equals("changeStatus")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getAll4Admin")){
				GetAllHouseKeeping4Admin(req,resp);
			}else if(doClass.equals("getById")){
				GetHouseKeepingById(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddHouseKeeping(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException, JSONException, ServletException, EntityNotFoundException, 
			ParseException{	
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.ENGLISH);
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String address = req.getParameter("address");
		Date birthday = formatter.parse(req.getParameter("birthday"));
		String bloodGroup = req.getParameter("bloodGroup");
		String city = req.getParameter("city");
		String country = req.getParameter("country");
		Date dateOfJoin = formatter.parse(req.getParameter("dateOfJoin"));
		String designation = req.getParameter("designation");
		String driverLicense = req.getParameter("driverLicense");
		String emergencyNumber = req.getParameter("emergencyNumber");
		String firstName = req.getParameter("firstName");
		String gender = req.getParameter("gender");
		String identificationNo = req.getParameter("identificationNo");
		String lastName = req.getParameter("lastName");
		String mobile = req.getParameter("mobile");
		String passportNumber = req.getParameter("passportNumber");
		String phone = req.getParameter("phone");
		String socialSecurityCard = req.getParameter("socialSecurityCard");
		String state = req.getParameter("state");
		String zipCode = req.getParameter("zipCode");
		new HouseKeepingUserLogic(req, resp).Add(hotelId,address,birthday,bloodGroup,
				city,country,dateOfJoin,designation,driverLicense,emergencyNumber,firstName,
				gender,identificationNo,lastName,mobile,passportNumber,phone,
				socialSecurityCard,state,zipCode);
		
		GetAllHouseKeeping4Admin(req, resp);
	}

	private void EditHouseKeeping(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException, JSONException, ServletException, EntityNotFoundException, 
			ParseException{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.ENGLISH);	
		long id = Long.parseLong(req.getParameter("id"));
		String address = req.getParameter("address");
		Date birthday = formatter.parse(req.getParameter("birthday"));
		String bloodGroup = req.getParameter("bloodGroup");
		String city = req.getParameter("city");
		String country = req.getParameter("country");
		Date dateOfJoin = formatter.parse(req.getParameter("dateOfJoin"));
		String designation = req.getParameter("designation");
		String driverLicense = req.getParameter("driverLicense");
		String emergencyNumber = req.getParameter("emergencyNumber");
		String firstName = req.getParameter("firstName");
		String gender = req.getParameter("gender");
		String identificationNo = req.getParameter("identificationNo");
		String lastName = req.getParameter("lastName");
		String mobile = req.getParameter("mobile");
		String passportNumber = req.getParameter("passportNumber");
		String phone = req.getParameter("phone");
		String socialSecurityCard = req.getParameter("socialSecurityCard");
		String state = req.getParameter("state");
		String zipCode = req.getParameter("zipCode");
		new HouseKeepingUserLogic(req, resp).Edit(id,address,birthday,bloodGroup,
				city,country,dateOfJoin,designation,driverLicense,emergencyNumber,firstName,
				gender,identificationNo,lastName,mobile,passportNumber,phone,
				socialSecurityCard,state,zipCode);

		GetAllHouseKeeping4Admin(req, resp);

	}

	private void DeleteHouseKeeping(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String id = req.getParameter("id");
		HouseKeepingUserLogic house = new HouseKeepingUserLogic(req, resp);
		for(int i = 0; i < id.split(",").length ; i++){
			house.Delete(Long.parseLong(id.split(",")[i]));
		}

		GetAllHouseKeeping4Admin(req, resp);
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long id = Long.parseLong(req.getParameter("id"));

		HouseKeepingUser obj = new HouseKeepingUserLogic(req, resp).ChangeStatus(id);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllHouseKeeping4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<HouseKeepingUser> obj = new HouseKeepingUserLogic(req, resp).GetAllHouseKeepingUser4AdminLimit(hotelId,offset,line);
		double dataSize = new HouseKeepingUserLogic(req, resp).GetAllHouseKeepingUser4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetHouseKeepingById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		HouseKeepingUser pos = new HouseKeepingUserLogic(req, resp).GetHouseKeepingUser(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(pos));
	}
}
